<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" width="600px" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title">
        <el-form ref="form" :model="form" :rules="rules" label-width="auto">
          <el-row>
            <el-col :span="12">
              <el-form-item label="中文支付方式名称 " prop="payNameCn">
                <el-input v-model="form.payNameCn" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="英文支付方式名称" prop="payNameEn">
                <el-input v-model="form.payNameEn" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="英文支付方式描述">
                <el-input v-model="form.payDescCn" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="英文支付方式描述">
                <el-input v-model="form.payDescEn" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="支付固定优惠">
                <el-input-number v-model.number="form.fixedDiscount" :min="0" :max="999999" controls-position="right" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支付折扣">
                <el-input-number v-model.number="form.rateDiscount" :min="0" :max="999999" controls-position="right" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="支付方式图标">
                <el-upload
                  ref="my-upload"
                  :on-success="handleSuccess"
                  :on-error="handleError"
                  :headers="headers"
                  :action="imagesUploadApi"
                  :data="type"
                  list-type="picture"
                  :limit="1"
                  class="avatar-uploader"
                  :show-file-list="false"
                >
                  <img v-if="imageUrl" :src="imageUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支付类型" prop="payType">
                <el-select v-model="form.payType" filterable placeholder="请选择">
                  <el-option
                    v-for="item in dict.payType"
                    :key="item.id"
                    :label="item.dictValue"
                    :value="item.dictKey"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否线上支付">
                <el-switch v-model="form.online" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否启用">
                <el-switch v-model="form.enable" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="normal" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" stripe :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="payNameCn" label="支付方式名稱" width="200">
          <template slot-scope="scope">
            <div class="productInfo">
              <img :src="baseApi + scope.row.icon" class="tableImg" alt="">
              <span>{{ scope.row.payNameCn }}</span>
              <span>{{ scope.row.payNameEn }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="payDescCn" label="支付方式描述" width="300">
          <template slot-scope="scope">
            <div class="productInfo">
              <span>{{ scope.row.payDescCn }}</span>
              <span>{{ scope.row.payDescEn }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="fixedDiscount" label="支付固定優惠" />
        <el-table-column prop="rateDiscount" label="支付折扣" />
        <el-table-column prop="payType" label="支付類型">
          <template slot-scope="scope">
            {{ dict.label.payType[scope.row.payType] }}
          </template>
        </el-table-column>
        <el-table-column prop="online" label="是否綫上支付">
          <template slot-scope="scope">
            <span>{{ scope.row.online === true?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="enable" label="是否啓用">
          <template slot-scope="scope">
            <span>{{ scope.row.enable === true?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','payMethod:edit','payMethod:del'])" label="操作" width="150px">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPayMethod from '@/api/fms/payMethod'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'

const defaultForm = { payNameCn: null, payNameEn: null, payDescCn: null, payDescEn: null, fixedDiscount: 0, rateDiscount: 0, payType: 'wx', icon: null, online: false, enable: false }
export default {
  name: 'PayMethod',
  components: { pagination, crudOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['payType'],
  cruds() {
    return CRUD({ title: '支付类型', url: 'payMethod/page', sort: 'id:desc', crudMethod: { ...crudPayMethod }})
  },
  data() {
    return {
      imageUrl: '',
      headers: {
        'Authorization': getToken()
      },
      type: { type: 4 },
      permission: {
        add: ['admin', 'payMethod:add'],
        edit: ['admin', 'payMethod:edit'],
        del: ['admin', 'payMethod:del']
      },
      rules: {
        payNameCn: [
          { required: true, message: '中文支付方式名称 不能为空', trigger: 'blur' }
        ],
        payNameEn: [
          { required: true, message: '英文支付方式名称不能为空', trigger: 'blur' }
        ],
        payType: [
          { required: true, message: '支付类型不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'payNameCn', display_name: '中文支付方式名称 ' }
      ]
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.imageUrl = this.baseApi + form.icon
    },
    [CRUD.HOOK.afterEditCancel]() {
      this.imageUrl = ''
    },
    [CRUD.HOOK.afterAddCancel]() {
      this.imageUrl = ''
    },
    [CRUD.HOOK.afterSubmit]() {
      this.imageUrl = ''
      this.clearFiles()
    },
    sortChange(column) {
      if (column.order === 'ascending') {
        this.crud.sort = [column.prop + ':asc']
      }
      if (column.order === 'descending') {
        this.crud.sort = [column.prop + ':desc']
      }
      this.crud.refresh()
    },
    handleSuccess(res, file) {
      if (res.code === 0) {
        this.form.icon = res.data.url
      } else {
        this.$message.error('图片上传失败,请重新上传')
      }
      this.imageUrl = URL.createObjectURL(file.raw)
      this.clearFiles()
    },
    handleError() {
      this.$message.error('图片上传失败,请重新上传')
    },
    clearFiles() {
      this.$refs['my-upload'].clearFiles()
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.productInfo span {
  display: block;
  line-height: 16px;
}
.tableImg {
  width: 30px;
  margin-right: 0px;
  float: left;
}
</style>
